 function getCurrentStyleWhole(sId, sStyle) {
	var oStyle = getContentStyle(sId);
	var sRet = "false";
	switch(sStyle) 
	{
		case "bold":
			if("@^" == oStyle.bold) {
				sRet = "true";
			}
			break;
		case "italic":
			if("@^" == oStyle.italic) {
				sRet = "true";
			}
			break;
		case "lineThrough":
			if("@^" == oStyle.lineThrough) {
				sRet = "true";
			}
			break;
		case "underline":
			if("@^" == oStyle.underline) {
				sRet = "true";
			}
			break;
		case "justify":
			sRet = "left"; // default
			sRet = getCurrentStyleTextAlign(oStyle);
			break;
		case "fontFamily":
			sRet = oStyle.fontFamily.toString();
			break;
		case "fontSize":
			var fontSize = parseInt(oStyle.fontSize, 10);
			if ( oStyle.fontSize.match(/px$/)) {
				fontSize = parseInt(oStyle.fontSize, 10) * 72 / 96;
			} 
			fontSize = Math.ceil(fontSize);
			sRet = fontSize.toString();
			break;
		case "fontColor":
			sRet = oStyle.fontColor.toString();
			break;
		case "lineHeight":
			sRet = "1.5";
			sRet = oStyle.lineHeight.toString();
			break;
		case "letterSpacing":
			sRet = "normal";
			sRet = oStyle.letterSpacing.toString();
			break;
		case "textTransform":
			sRet = "none";
			sRet = oStyle.textTransform.toString();
			break;
		default:
			break;
	}

	return sRet;
}

function isHuskyReady() {
	if(oHuskyDs.appStatus == nhn.husky.APP_STATUS.READY) {
		return "true";
	}
	
	return "false";
}

function execWhenReady(func, type, param) {	
	if(oHuskyDs.appStatus == nhn.husky.APP_STATUS.READY) {
		return execFunction(func, type, param);
		//oHuskyDs.exec(func);
	} 	
	else {
		var myInterval = setInterval(function(){
			if(oHuskyDs.appStatus == nhn.husky.APP_STATUS.READY) {
				endInterval(myInterval);
				//return jindo.$Fn(this.execFunction(func, type, param), window).bind();
				return execFunction(func, type, param);
				//oHuskyDs.exec(func); 
			}
		}
		, 50 );
	}
}

function endInterval(interval) {
	clearInterval(interval);
}

function execFunction(func, type, param) {
	if ("get" == func.toLowerCase()) {
		return getFunction(type, param);
	} 
	else if ("set" == func.toLowerCase()) {
		return setFunction(type, param);
	}
}

function getFunction(type, param) {
	if ("contents" == type) {
		retString = oHuskyDs.getContents();
	}
	else {
		retString = getCurrentStyle(param);
	}
	//console.log(retString.toString());
}

function setFunction(type, param) {
	if ("contents" == type.toLowerCase()) {
		return oHuskyDs.setContents(param);
	}
	else {
		//oHuskyDs.exec("GET_DS_SELECTION");
		return oHuskyDs.exec(type, param);
	}
}

function getCurrentStyle(sStyle) {
	oHuskyDs.exec('CHECK_STYLE_CHANGE');
	var oStyle = oHuskyDs.getCurrentStyle();
	var sRet = "false";
	switch(sStyle) 
	{
		case "bold":
			if("@^" == oStyle.bold) {
				sRet = "true";
			}
			break;
		case "italic":
			if("@^" == oStyle.italic) {
				sRet = "true";
			}
			break;
		case "lineThrough":
			if("@^" == oStyle.lineThrough) {
				sRet = "true";
			}
			break;
		case "underline":
			if("@^" == oStyle.underline) {
				sRet = "true";
			}
			break;
		case "justify":
			sRet = "left"; // default
			sRet = getCurrentStyleTextAlign(oStyle);
			break;
		case "fontFamily":
			sRet = oStyle.fontFamily.toString();
			break;
		case "fontSize":
			var fontSize = parseInt(oStyle.fontSize, 10);
			if ( oStyle.fontSize.match(/px$/)) {
				fontSize = parseInt(oStyle.fontSize, 10) * 72 / 96;
			} 
			fontSize = Math.ceil(fontSize);
			sRet = fontSize.toString();
			break;
		case "fontColor":
			sRet = oStyle.fontColor.toString();
			break;
		case "backgroundColor":
			sRet = oStyle.backgroundColor.toString();
			break;
		case "lineHeight":
			sRet = "1.5";
			sRet = oStyle.lineHeight.toString();
			break;
		case "letterSpacing":
			sRet = "normal";
			sRet = oStyle.letterSpacing.toString();
			break;
		case "textTransform":
			sRet = "none";
			sRet = oStyle.textTransform.toString();
			break;
		default:
			break;
	}

	return sRet;
}

function getCurrentStyleTextAlign(oStyle)
{
	if("@^" == oStyle.justifyleft) {
		return "left";
	} else if("@^" == oStyle.justifycenter) {
		return "center";
	} else if("@^" == oStyle.justifyright) {
		return "right";
	} else if("@^" == oStyle.justifyfull) {
		return "justify";
	}
}
/*
function getFontSize() {
	var e = $A(oHuskyDs.oApp.getSelection()._getNodesInRange());
	var len = e.length();
	var node = e.get(0);
	var hStyle = $H();
	var style;
	
	if(node.tagName == "SPAN")
	{
		style = $Element(node).attr("style");
		//console.log(style);
	}
	else if(node.parentNode.tagName == "SPAN")  
	{
		style = $Element(node.parentNode).attr("style");
		//console.log("parent : " + style);
	}

	var aStyle = style.split(';');
	var len = aStyle.length;
	for(i = 0; i < len; i++)
	{
		sString = aStyle[i];
		aPair = sString.split(':');
		if((aPair[0]) && (aPair[1]))
		{
			hStyle.add(aPair[0].replace(' ', ''), aPair[1].replace(' ', ''));
			//console.log(aPair[0] + " " + aPair[1]);
		}
	}

	if(hStyle.hasKey("font-size"))
	{
		return hStyle.$("font-size").toString();
	}
	
	return "";	
}
function getTextAlign() {
	var node = oHuskyDs.oApp.getSelection().commonAncestorContainer;
	if("body" == node.tagName.toLowerCase())
	{
		node = $A(oHuskyDs.oApp.getSelection()._getNodesInRange()).get(0);
	}
	var hStyle = $H();
	var style;

	// div 여러 번 덮혀 있을 때 처리 시 문제 발생 가능성 있음
	while("DIV" != node.tagName && ("DIV" != node.parentNode.tagName || "SPAN" != node.parentNode.tagName))
	{
		node = node.parentNode;
	}
	style = $Element(node).attr("style");

	var aStyle = style.split(';');
	var len = aStyle.length;
	for(i = 0; i < len; i++)
	{
		sString = aStyle[i];
		aPair = sString.split(':');
		if((aPair[0]) && (aPair[1]))
		{
			hStyle.add(aPair[0].replace(' ', ''), aPair[1].replace(' ', ''));
			//console.log(aPair[0] + " " + aPair[1]);
		}
	}

	if(hStyle.hasKey("text-align"))
	{
		return hStyle.$("text-align").toString();
	}
	
	return "";
}

function getStyle(styleType) {
	var e = $A(oHuskyDs.oApp.getSelection()._getNodesInRange());
	var len = e.length();
	var node = e.get(0);
	var hStyle = $H();
	var style;
	
	if(node.tagName == "SPAN")
	{
		style = $Element(node).attr("style");
		//console.log(style);
	}
	else if(node.parentNode.tagName == "SPAN")  
	{
		style = $Element(node.parentNode).attr("style");
		//console.log("parent : " + style);
	}
	
	var aStyle = style.split(';');
	var len = aStyle.length;
	for(i = 0; i < len; i++)
	{
		sString = aStyle[i];
		aPair = sString.split(':');
		if((aPair[0]) && (aPair[1]))
		{
			hStyle.add(aPair[0].replace(' ', ''), aPair[1].replace(' ', ''));
			//console.log(aPair[0] + " " + aPair[1]);
		}
	}

	if(hStyle.hasKey(styleType))
	{
		return hStyle.$(styleType).toString();
	}
	
	return "";
}
*/

//객체의 넓이 
function getDivClientWidth(strObjID) {
	var objDiv = $(strObjID);	

	var nWidth = 0;	

	if ( objDiv ) {
		nWidth = objDiv.clientWidth;
	
	}
	return nWidth;
}

//객체의 높이 리턴
function getDivClientHeight(strObjID) {
	var objDiv = $(strObjID);	
	
	var nHeight = 0;
	
	if ( objDiv ) {
		nHeight = objDiv.clientHeight;
	}
	return nHeight;
}

function getPlainText(sObjID) {
	if ($(sObjID)) {
		return $Element(sObjID).text();
	} else {
		return "";
	}
}

function setDSContents(sObjID) {
	var sHtml = $(sObjID).innerHTML;
	oHuskyDs.setContents(sHtml);
}

function destroyDSEditor(sObjID) {
	// 1. objectSelect
	var objDiv = $(sObjID);
	if (objDiv) {
		var objIframe =  $$.getSingle("iframe.se2_input_wysiwyg", objDiv);
		var sContentHTML = objIframe.contentWindow.document.body.innerHTML;
		
		// 2. class 제거
		objDiv.innerHTML = sContentHTML;
		$Element(objDiv).removeClass("husky_seditor_editing_area_container");
	}	
}

function createDSEditor(sObjID, bFirstShowUp) {
	// 1. objectSelect
	var uiText = $(sObjID);
	$Element(uiText).addClass("husky_seditor_editing_area_container");
	
	// 2. InnerHTML save
	var sInnerHTML = uiText.innerHTML;

	sInnerHTML =  $S(sInnerHTML).trim();


	// 3. Iframe injection
	uiText.innerHTML = '<iframe	src="smart_editor2_inputarea.htm" id="se2_iframe" name="se2_iframe"	class="se2_input_wysiwyg" style="display: block; width: 102px; height: 20px;" frameborder="0" scrolling="no">';
	return createSEditor2(sInnerHTML, bFirstShowUp);
}

function createSEditor2(sContents, bFirstShowUp, elIRField, htParams, elSeAppContainer) {
		if(!window.$Jindo){
			parent.document.body.innerHTML="진도 프레임웍이 필요합니다.<br>\n<a href='http://dev.naver.com/projects/jindo/download'>http://dev.naver.com/projects/jindo/download</a>에서 jindo.min.js를 다운로드 받아 /js 폴더에 복사 해 주세요.";
			return;
		}
		elIRField = (elIRField || jindo.$("dsIR"));
		elIRField.style.display = "none";

		var elAppContainer = (elSeAppContainer || jindo.$$.getSingle("div.husky_seditor_editing_area_container"));
		if (!elAppContainer) {
		    parent.document.body.innerHTML += "elAppContainer 가 null입니다.";
		}
		var oWYSIWYGIFrame = jindo.$$.getSingle("IFRAME.se2_input_wysiwyg", elAppContainer);
		if (!oWYSIWYGIFrame) {
		    parent.document.body.innerHTML += "oWYSIWYGIFrame 가 null입니다.";
		}
		
		var oIRTextarea = elIRField?elIRField:jindo.$$.getSingle("TEXTAREA.blind", elEditingArea);
		if (!oIRTextarea) {
		    parent.document.body.innerHTML += "oIRTextarea 가 null입니다.";
		}
		
		if(!htParams){ htParams = {}; }
		htParams.elAppContainer = elAppContainer;												// 에디터 UI 최상위 element 셋팅 
		htParams.oNavigator = jindo.$Agent().navigator();										// navigator 객체 셋팅

		var oHusky = new nhn.husky.HuskyCore(htParams);
		if (!oHusky) {
		    parent.document.body.innerHTML += "oHusky 가 null입니다.";
		}

		var nEditingAreaHeight = (elAppContainer.style.height || elAppContainer.offsetHeight + "px");
		var nEditingAreaWidth = (elAppContainer.style.width || elAppContainer.offsetWidth + "px");

		oHusky.registerPlugin(new nhn.husky.CorePlugin(htParams?htParams.fOnAppLoad:null));	
		oHusky.registerPlugin(new nhn.husky.StringConverterManager());
		
		var htDimension = { nHeight:nEditingAreaHeight, nMinHeight:10, nMinWidth:10, nWidth : nEditingAreaWidth };
		jindo.$Element(oWYSIWYGIFrame).width(parseInt(nEditingAreaWidth) + 2); // iframe 폭 div 걸로 셋팅
		
		oHusky.registerPlugin(new nhn.husky.SE_EditingAreaManager("WYSIWYG", oIRTextarea, htDimension, null, document.body));
		oHusky.registerPlugin(new nhn.husky.SE_EditingArea_WYSIWYG(oWYSIWYGIFrame));			// Tab Editor 모드
		
		oHusky.registerPlugin(new nhn.husky.HuskyRangeManager(oWYSIWYGIFrame));
		oHusky.registerPlugin(new nhn.husky.Utils());
		oHusky.registerPlugin(new nhn.husky.SE2M_UtilPlugin());
		oHusky.registerPlugin(new nhn.husky.SE_WYSIWYGStyler());

		oHusky.registerPlugin(new nhn.husky.Hotkey());											// 단축키
		oHusky.registerPlugin(new nhn.husky.SE_WYSIWYGStyleGetter());							// 커서 위치 스타일 정보 가져오기
		oHusky.registerPlugin(new nhn.husky.SE_WYSIWYGEnterKey("P"));							// 엔터 시 처리, 현재는 P로 처리
		
		oHusky.registerPlugin(new nhn.husky.SE2M_LineStyler());								// 표 테두리 스타일
		oHusky.registerPlugin(new nhn.husky.SE2M_StyleRemover());								// 글자효과 없애기

		oHusky.registerPlugin(new nhn.husky.SE2M_ExecCommand(oWYSIWYGIFrame));
		oHusky.registerPlugin(new nhn.husky.MessageManager(oMessageMap));

		// ADD by hangsu.cho 2011-08-16 control Body-Style
		oHusky.registerPlugin(new nhn.husky.SE_WYSIWYGBodyStyleSetter());
		//oHusky.registerPlugin(new nhn.husky.SE2M_Hyperlink(oMessageMap));					// 링크

		oHusky.run({
		    fnOnAppReady: function () {
				//Iframe내 bodystyle을 Container의 style로 copy
				oHusky.copyBodyStyle(); 
				oHusky.setBodyStyle("position", "");
				oHusky.setBodyStyle("left", "");
				oHusky.setBodyStyle("top", "");
				oHusky.setBodyStyle("border", "");
		        oHusky.setContents(sContents);
				
		        oWYSIWYGIFrame.contentDocument.body.contentEditable = false;
				oWYSIWYGIFrame.contentDocument.body.contentEditable = true;
				
				if (true == bFirstShowUp)
				{
					oHusky.exec("DS_FOCUS_LAST_NODE");
				}
				else
				{
					oHusky.exec("DS_FOCUS_LAST_NODE", [true]);
				}
		        // false, true
		
				//oHusky.exec("DS_FOCUS_LAST_NODE", [false]);
				
		    }
		});

		return oHusky;
}

function test() {
    var oDiv = parent.document.createElement("div");
    oDiv.style.border = "1px solid red";
    parent.document.body.appendChild(oDiv);
}

function showEditor(id) {
    $Element(jindo.$$.getSingle("div.husky_seditor_editing_area_container")).width($Element(jindo.$(id)).width());
    $Element(jindo.$$.getSingle("div.husky_seditor_editing_area_container")).height($Element(jindo.$(id)).height());
    $Element(jindo.$$.getSingle("div.husky_seditor_editing_area_container")).left($Element(jindo.$(id)).left());
    $Element(jindo.$$.getSingle("div.husky_seditor_editing_area_container")).top($Element(jindo.$(id)).top());
    $Element(jindo.$$.getSingle("div.husky_seditor_editing_area_container")).display("block");
}
